package ldk.easytouch.example.activity;

import android.annotation.SuppressLint;
import android.support.annotation.Nullable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import java.lang.reflect.Field;

import ldk.easytouch.activity.BaseActivity;
import ldk.easytouch.example.R;
import ldk.easytouch.example.helper.RecentTaskHelper;
import ldk.easytouch.helper.PositionHelper;
import ldk.easytouch.interfaces.ResultCallback;
import ldk.easytouch.utils.ETLog;
import ldk.easytouch.utils.OSUtil;

/**
 * Created by ldkxingzhe on 2016/9/13.
 */
public class OriginActivity extends BaseActivity implements ResultCallback<Boolean> {
    @SuppressWarnings("unused")
    private static final String TAG = "OriginActivity";

    private ImageView mPoint;
    private RecentTaskHelper mTaskHelper;
    @Override
    public void onCreate() {
        super.onCreate();
        setContentView(R.layout.activity_origin);
        mPoint = findView(R.id.iv_point);
        mPoint.setOnTouchListener(new PointTouchListener());
        mTaskHelper = new RecentTaskHelper(getContext(), this);
    }

    @Override
    public boolean onUpdateWindowLayoutParams(WindowManager.LayoutParams params) {
        setFlagNotTouchOutSide(params);
        adjustCenter(params);
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        mTaskHelper.onResume();
    }

    @Override
    public void onStop() {
        super.onStop();
        mTaskHelper.onStop();
    }

    @Override
    public void onResult(int resultCode, @Nullable Boolean result) {
        // 是否在桌面上的回调, result --> true  在桌面上
//        getContentView().setVisibility(result ? View.VISIBLE : View.INVISIBLE);
    }

    private class PointTouchListener implements View.OnTouchListener {
        private GestureDetector mGestureDetector;
        private boolean mHasLongPressed;

        @SuppressLint("HandlerLeak")
        public PointTouchListener(){
            mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener(){
                @Override
                public boolean onDown(MotionEvent e) {
                    mHasLongPressed = false;
                    return super.onDown(e);
                }

                @Override
                public boolean onSingleTapConfirmed(MotionEvent e) {
                    ETLog.v(TAG, "point onSingleTapConfirmed");
                    startActivity(new MenuActivity());
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    ETLog.v(TAG, "point onLongPressed");
                    mHasLongPressed = true;
                    OSUtil.vibrator(getContext());
                    try {
                        Field mInLongPressField = GestureDetector.class.getDeclaredField("mInLongPress");
                        mInLongPressField.setAccessible(true);
                        mInLongPressField.set(mGestureDetector, false);
                    } catch (NoSuchFieldException e1) {
                        ETLog.e(TAG, e1.getMessage(), e1);
                    } catch (IllegalAccessException e1) {
                        ETLog.e(TAG, e1.getMessage(), e1);
                    }
                }

                @Override
                public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                    if(!mHasLongPressed) return true;
                    int centerX = (int) e2.getRawX();
                    int centerY = (int) e2.getRawY();
                    PositionHelper.getInstance().setCenter(centerX, centerY);
//                    ETLog.v(TAG, "position onScroll, and centerX is %d, centerY is %d.", centerX, centerY);
                    return super.onScroll(e1, e2, distanceX, distanceY);
                }
            });
        }
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mGestureDetector.onTouchEvent(event);
            return true;
        }
    }
}
